package com.httpClient.chapter1;

import com.sky.JULLog.MyJulLogger;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 *  这个{@link ResponseHandler    }  其实就是为了代码复用, 自己写一个一点问题没有;
 *  <strong>它并不会帮你处理什么</strong>,  仅仅是提供一个接口, 然后丢给你自己处理;
 *
 *  date : 2024-06-18
 *  <p>
 * @author : wushikai
 *
 */
public class ResponseHandlerHttpClient {


    private static Logger log = MyJulLogger.getLogger(ResponseHandlerHttpClient.class);

    private static HttpClient httpclient;
    private static HttpPost httpPost;

    static {

        httpclient = new DefaultHttpClient();
        httpPost = new HttpPost("http://localhost:8080/xy/expose/merDetail");

        List<NameValuePair> formParams = new ArrayList<>();
        formParams.add(new BasicNameValuePair("sp.lk.merchant_name", "广西"));
        UrlEncodedFormEntity paramEntity = new UrlEncodedFormEntity(formParams, StandardCharsets.UTF_8);
        httpPost.setEntity(paramEntity);
    }


    public static void main(String[] args) throws IOException {

        doRequestWithInnerHandler();
        doRequestWithHandlerClass();

    }


    private static void doRequestWithInnerHandler( ) throws IOException {


        ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
            public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    return EntityUtils.toByteArray(entity);
                } else {
                    return new byte[0];
                }
            }
        };
        byte[] response = httpclient.execute(httpPost, handler);
        log.info("response.length ==> " + response.length);
        log.info("response.content ==> " + new String(response, StandardCharsets.UTF_8));

    }


    private static void doRequestWithHandlerClass( ) throws IOException {
        byte[] response = httpclient.execute(httpPost, new ByteRespHandler());
        log.info("response.length ==> " + response.length);
        log.info("response.content ==> " + new String(response, StandardCharsets.UTF_8));


    }


}



class ByteRespHandler implements ResponseHandler<byte[]> {
    @Override
    public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            return EntityUtils.toByteArray(entity);
        } else {
            return new byte[0];
        }

    }
}
